#include "lgfilteritem.h"
#include "ui_lgfilteritem.h"

#include <QMenu>

LGFilterItem::LGFilterItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LGFilterItem)
    , m_count(0)
    , m_index(0)
    , m_blocked(false)
{
    ui->setupUi(this);
}

LGFilterItem::~LGFilterItem()
{
    delete ui;
}

LGFilterItem *LGFilterItem::newFilterItem(bool checked, QString text)
{
    auto item = new LGFilterItem;
    item->ui->check->setChecked(checked);
    item->ui->text->setText(text);
    item->setCount(1);
    return item;
}

bool LGFilterItem::isChecked()
{
    return ui->check->isChecked();
}

QString LGFilterItem::text()
{
    return ui->text->text();
}

void LGFilterItem::on_check_stateChanged(int arg1)
{
    emit onStateChanged();
}

int LGFilterItem::index() const
{
    return m_index;
}

void LGFilterItem::setIndex(int newIndex)
{
    m_index = newIndex;
}

int LGFilterItem::count() const
{
    return m_count;
}

void LGFilterItem::setCount(int newCount)
{
    m_count = newCount;
    ui->count->setText(QString::number(m_count)+"项");
}

void LGFilterItem::increment()
{
    setCount(m_count+1);
}


void LGFilterItem::on_LGFilterItem_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu;
    if (!m_blocked)
        // 屏蔽
        menu.addAction(ui->action_block_item);
    else
        // 解除
        menu.addAction(ui->action_unblock_item);
    menu.exec(QCursor::pos());
}


void LGFilterItem::on_action_block_item_triggered()
{
    m_blocked = true;
    emit onStateChanged();
}

void LGFilterItem::on_action_unblock_item_triggered()
{
    m_blocked = false;
    emit onStateChanged();
}

bool LGFilterItem::isBlocked()
{
    return m_blocked;
}



